package cz.muni.fi.pa036.kiss.serviceimpl;

import cz.muni.fi.pa036.kiss.Entity.KolejAssembler;
import cz.muni.fi.pa036.kiss.Entity.KolejDTO;
import cz.muni.fi.pa036.kiss.Entity.SluzbaKoleje;
import cz.muni.fi.pa036.kiss.Entity.SluzbaKolejeAssembler;
import cz.muni.fi.pa036.kiss.Entity.SluzbaKolejeDTO;
import cz.muni.fi.pa036.kiss.dao.SluzbaKolejeDAO;
import cz.muni.fi.pa036.kiss.service.SluzbaKolejeService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author peter
 */
public class SluzbaKolejeServiceImpl implements SluzbaKolejeService{

    @Autowired
    private SluzbaKolejeDAO sluzbaDAO;
    @Autowired
    private SluzbaKolejeAssembler sluzbaassembler;
    @Autowired
    private KolejAssembler kolejassembler;
    
    @Transactional
    @Override
    public void create(SluzbaKolejeDTO sluzba) {
        if (sluzba == null) {
            throw new IllegalArgumentException("sluzba is null");
        }
        
        if (sluzba.getId() != null) {
            throw new IllegalArgumentException("sluzba.id is not null");
        }
        SluzbaKoleje maj = sluzbaassembler.createEntityfromDTO(sluzba);
        sluzbaDAO.create(maj);
        sluzba.setId(maj.getId());
    }

    @Transactional(readOnly = true)
    @Override
    public SluzbaKolejeDTO get(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("sluzba.Id is null");
        }
        
        SluzbaKoleje maj = sluzbaDAO.get(id);
        if (maj == null) {
            return null;
        }        
        return sluzbaassembler.createDTOfromEntity(maj);
    }

    @Transactional
    @Override
    public void update(SluzbaKolejeDTO sluzba) {
        if (sluzba == null) {
            throw new IllegalArgumentException("sluzba is null");
        }        
        if (sluzba.getId() == null) {
            throw new IllegalArgumentException("sluzba.id is null");
        }
        
        SluzbaKoleje maj = sluzbaassembler.createEntityfromDTO(sluzba);
        sluzbaDAO.update(maj);
    }

    @Transactional
    @Override
    public void remove(SluzbaKolejeDTO sluzba) {
        if (sluzba == null) {
            throw new IllegalArgumentException("sluzba is null");
        }        
        if (sluzba.getId() == null) {
            throw new IllegalArgumentException("sluzba.id is null");
        }
        
        SluzbaKoleje maj = sluzbaassembler.createEntityfromDTO(sluzba);
        sluzbaDAO.remove(maj);
    }

    @Transactional(readOnly = true)
    @Override
    public List<SluzbaKolejeDTO> findAll() {
        List<SluzbaKoleje> result = sluzbaDAO.findAll();
        List<SluzbaKolejeDTO> dTOList = new ArrayList();
        
        for (SluzbaKoleje maj : result) {
            dTOList.add(sluzbaassembler.createDTOfromEntity(maj));
        }
        return dTOList;
    }

    @Transactional(readOnly = true)
    @Override
    public List<SluzbaKolejeDTO> findByKolej(KolejDTO kolej) {
        if (kolej == null) {
            throw new IllegalArgumentException("kolej is null");
        }
        List<SluzbaKoleje> result = sluzbaDAO.findByKolej(kolejassembler.createEntityfromDTO(kolej));
        List<SluzbaKolejeDTO> ret = new ArrayList();
        for (SluzbaKoleje bp : result) {
            ret.add(sluzbaassembler.createDTOfromEntity(bp));
        }
        return ret;
    }

    @Transactional(readOnly = true)
    @Override
    public List<SluzbaKolejeDTO> findByName(String nazev) {
        if (nazev == null) {
            throw new IllegalArgumentException("nazev is null");
        }
        List<SluzbaKoleje> result = sluzbaDAO.findByName(nazev);
        List<SluzbaKolejeDTO> ret = new ArrayList();
        for (SluzbaKoleje bp : result) {
            ret.add(sluzbaassembler.createDTOfromEntity(bp));
        }
        return ret;
    }
    
}
